import pymysql

conn = None
cursor = None

try:
    conn = pymysql.connect(host="127.0.0.1",
                           port=3306,
                           user="root",
                           password="root",
                           database="books",
                           autocommit=False)
    cursor = conn.cursor()

    sql = "inset into t_book(id,title,pub_date) values(4,'西游记','1986-01-01');"

    cursor.execute(sql)
    print(cursor.rowcount)
    raise Exception("Error")

    # 4).新增一条英雄人物数据（name:孙悟空 gender:1 book_id:4）
    sql = "insert into t_hero(name,gender,book_id) values('孙悟空', 1, 4)"
    cursor.execute(sql)
    print(cursor.rowcount)
    conn.commit()
except Exception as e :
    conn.rollback()

    print(e)
finally:
    if cursor:
        cursor.close()
    if conn:
        conn.close()

